#include<iostream>
#include<vector>
using namespace std;


class Solution {
public:
    int candy(vector<int>& ratings) {
        int len = ratings.size();
        vector<int> candys(len, 1);
        for (int i = 1; i < len; i++) {
            if (ratings[i] > ratings[i - 1]) {
                candys[i] = candys[i - 1] + 1;
            }
        }
        for (int i = len - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                candys[i] = max(candys[i + 1] + 1, candys[i]);
            }
        }
        int ans = 0;
        for (auto e : candys) {
            ans += e;
        }
        return ans;
    }
};